﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RS.DAL.Wrapper;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;

namespace RS.DAL
{
    public class DALRSManager
    {
        public ResumeSubmitDTO SaveDockets(ResumeSubmitDTO rsDTO, Operations opertation)
        {
            string docketNumber = string.Empty;

            try
            {

                switch (opertation)
                {
                    #region Create Dockets
                    case Operations.Add:

                        DalHelper helper = new DalHelper();
                        SqlDatabase db = helper.GetDatabase(string.Empty);
                        using (DbConnection connection = db.CreateConnection())
                        {
                            connection.Open();
                            DbTransaction transaction = connection.BeginTransaction();
                            try
                            {
                                DalHelper dalhelper = new DalHelper();

                                #region Jsp_ResumeSubmitInsert
                                object[,] arParams = new object[10, 4];
                                arParams[0, 0] = "FirstName";
                                arParams[0, 1] = DbType.String;
                                arParams[0, 2] = rsDTO.FirstName;
                                arParams[0, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[1, 0] = "LastName";
                                arParams[1, 1] = DbType.String;
                                arParams[1, 2] = rsDTO.LastName;
                                arParams[1, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[2, 0] = "NationalityID";
                                arParams[2, 1] = DbType.Int32;
                                arParams[2, 2] = rsDTO.Nationality;
                                arParams[2, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[3, 0] = "SingaporeVisaStatusID";
                                arParams[3, 1] = DbType.Int32;
                                arParams[3, 2] = rsDTO.VisaStatus;
                                arParams[3, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[4, 0] = "ResumeTitle";
                                arParams[4, 1] = DbType.String;
                                arParams[4, 2] = rsDTO.ResumeTitle;
                                arParams[4, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[5, 0] = "ResumeText";
                                arParams[5, 1] = DbType.String;
                                arParams[5, 2] = rsDTO.ResumeText;
                                arParams[5, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[6, 0] = "ResumeFile";
                                arParams[6, 1] = DbType.Binary;
                                arParams[6, 2] = rsDTO.ResumeFile;
                                arParams[6, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[7, 0] = "MobileNo";
                                arParams[7, 1] = DbType.String;
                                arParams[7, 2] = rsDTO.ContactNumber;
                                arParams[7, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[8, 0] = "emailID";
                                arParams[8, 1] = DbType.String;
                                arParams[8, 2] = rsDTO.Email;
                                arParams[8, 3] = GeneralConstants.DIRECTION_INPUT_PARAMETER;

                                arParams[9, 0] = "nResumeID";
                                arParams[9, 1] = DbType.Int32;
                                arParams[9, 2] = rsDTO.ResumeID;
                                arParams[9, 3] = GeneralConstants.DIRECTION_OUTPUT_PARAMETER;

                                StringBuilder strBuilder = new StringBuilder();
                                strBuilder = dalhelper.dbExecuteNonQueryInOutParameter(DBConstants.ResumeSubmit.Insert, arParams, db, transaction);

                                if (strBuilder.ToString() == string.Empty)
                                {
                                    transaction.Rollback();
                                    throw new Exception("ERROR_100");
                                }
                                else
                                {
                                    string strResumeID = strBuilder.ToString().TrimStart(':');
                                    rsDTO.ResumeID = Convert.ToInt32(strResumeID);
                                }
                                #endregion

                                foreach (ObjBase objQua in rsDTO.Qualifications)
                                {
                                    #region Jsp_ResumeCertificationInsert
                                    arParams = new object[2, 3];
                                    arParams[0, 0] = "nResumeID";
                                    arParams[0, 1] = DbType.Int32;
                                    arParams[0, 2] = rsDTO.ResumeID;

                                    arParams[1, 0] = "@vQualification";
                                    arParams[1, 1] = DbType.String;
                                    arParams[1, 2] = objQua.Name;

                                    //insert the data into the product line table 
                                    if (!dalhelper.dbExecuteNonQuery(DBConstants.ResumeQualifications.Insert, arParams, db, transaction))
                                    {
                                        //transaction.Rollback();
                                        throw new Exception("ERROR_200");
                                    }
                                    #endregion

                                }

                                foreach (ObjBase objCert in rsDTO.ProfCertifications)
                                {
                                    #region Jsp_ResumeCertificationInsert
                                    arParams = new object[2, 3];
                                    arParams[0, 0] = "nResumeID";
                                    arParams[0, 1] = DbType.Int32;
                                    arParams[0, 2] = rsDTO.ResumeID;

                                    arParams[1, 0] = "nCertification";
                                    arParams[1, 1] = DbType.String;
                                    arParams[1, 2] = objCert.Name;

                                    //insert the data into the product line table 
                                    if (!dalhelper.dbExecuteNonQuery(DBConstants.ResumeCertification.Insert, arParams, db, transaction))
                                    {
                                        //transaction.Rollback();
                                        throw new Exception("ERROR_300");
                                    }

                                }

                                    #endregion

                                //    arParams[10, 0] = "iDirtyFlag";
                                //    arParams[10, 1] = DbType.Int16;
                                //    arParams[10, 2] = productDTO.DirtyFlag;

                                //    arParams[11, 0] = "nDoPrice";
                                //    arParams[11, 1] = DbType.Decimal;
                                //    arParams[11, 2] = productDTO.Price;


                                //    //insert the data into the product line table 
                                //    if (!dalhelper.dbExecuteNonQuery(StoredProcedureConstants.Dockets.EBSP_DOCKETS_SAVE_DOCKETS_LINE, arParams, db, transaction))
                                //    {
                                //        //transaction.Rollback();
                                //        throw new WPFatalException(WPExceptionCode.DOCKETS_SELECTED_NOT_ABLE_TO_SAVE_DUE_TO_EXCEED_LIMIT_OR_MORE_THAN_ONE_TIME_SAME_PRODUCT);
                                //    }

                                //}
                                //#endregion


                                transaction.Commit();
                                //docketsDTO.DocketNo = docketNumber;
                            }
                            catch (Exception ex)
                            {
                                transaction.Rollback();
                                connection.Close();
                                throw ex;
                            }
                        }
                    #endregion
                        break;



                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return rsDTO;
        }

    }
}
